package views
{
	import com.oosterwijk.irc.FlexIrcClient;
	import com.oosterwijk.irc.event.ServerEvent;
	
	import qnx.ui.core.Container;
	import qnx.ui.core.ContainerFlow;
	import qnx.ui.core.SizeUnit;
	import qnx.ui.data.DataProvider;
	import qnx.ui.events.ListEvent;
	import qnx.ui.listClasses.List;
	import qnx.ui.listClasses.ListSelectionMode;
	import qnx.ui.listClasses.ScrollDirection;
	import qnx.ui.listClasses.ScrollPane;

	public class ChannelList extends BaseView
	{
		private var _chList:List;
		private var _flexIrcClient:FlexIrcClient = null;
		public function ChannelList(client:FlexIrcClient)
		{
			super();
			_flexIrcClient = client;
			configUI();
		}
		
		private function configUI():void
		{
			
			_chList = new List();            
			//chList.setPosition(0, 100);            
			_chList.size = 100;
			_chList.sizeUnit = SizeUnit.PERCENT;
			_chList.columnWidth = 1020;
			_chList.setSkin(views.ChannelListRenderer);
			
			//set the dataProvider
			_chList.allowDeselect = false;
			_chList.selectionMode = ListSelectionMode.SINGLE;
			_chList.scrollDirection = ScrollDirection.VERTICAL;
			//_chList.dataProvider = new DataProvider(_chArray);
			_chList.addEventListener(ListEvent.ITEM_CLICKED, onListClick);

			var mainContainer:Container = new Container();
			//mainContainer.margins = Vector.<Number>([2,2,2,2]);
			mainContainer.flow = ContainerFlow.HORIZONTAL;
			//mainContainer.debugColor = 0xFFCC00;
			this.addChild(mainContainer);
			
			mainContainer.addChild(_chList);
			mainContainer.setSize(1024, 540);
		}
		/**
		 * Public getter and setter for the IRC Client Connection Object
		 */
		public function get flexIrcClient():FlexIrcClient
		{
			return _flexIrcClient;  
		}
		public function set flexIrcClient(value:FlexIrcClient):void
		{
			_flexIrcClient = value;
		}
		
		public function registerListeners():void
		{
			_flexIrcClient.addEventListener(FlexIrcClient.DISCONNECT_EVENT,onDisconnected);
		}
		
		protected function onDisconnected(event:ServerEvent):void
		{
			this._chList.dataProvider.removeAll();
		}
		private function onListClick(event:ListEvent):void
		{
			if(_flexIrcClient){
				var obj:Object = event.data;
				_flexIrcClient.joinChannel(obj.name as String);
			}
		}
		
		public function setChannelArray(chArray:Array):void
		{
			chArray.sortOn("name");
			this._chList.dataProvider = new DataProvider(chArray);
		}
	}
}